//
//  HQMineViewController.swift
//  TodayNews
//
//  Created by 郝庆 on 16/8/22.
//  Copyright © 2016年 haoqing. All rights reserved.
//

import UIKit

let mineCellID = "HQMineCell"

class HQMineViewController: UITableViewController {
    
    private lazy var cells = [AnyObject]()
    
    // 隐藏导航条
    override func viewWillAppear(animated: Bool) {
        navigationController?.navigationBarHidden = true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 从 plist 加载数据
        loadCellFromPlist()
        
        // 设置UI
        setupUI()
    }
    
    // MARK: - 从 plist 加载数据
    private func loadCellFromPlist() {
        let path = NSBundle.mainBundle().pathForResource("HQMineCellPlist.plist", ofType: nil)
        
        let cellPlist = NSArray.init(contentsOfFile: path!)
        
        for array in cellPlist! {
            let arrayDict = array as! NSArray
            var modelArr = [AnyObject]()
            for dict in arrayDict {
                // 字典转模型
                let cell = HQMineCellModel(dict: dict as! [String : AnyObject])
                modelArr.append(cell)
            }
            cells.append(modelArr)
        }
    }
    
    // MARK: - 设置UI
    private func setupUI() {
        // 背景色
        view.backgroundColor = HQGlobalColor()
        
        // 注册
        tableView.registerNib(UINib(nibName: String(HQMineCell), bundle: nil), forCellReuseIdentifier: mineCellID)
    
        // footerView
        let footerView = UIView()
        tableView.tableFooterView = footerView
        footerView.hq_height = kMargin
        
        // cell的高度
        tableView.rowHeight = kMineCellH
        
        // 影藏分割线
        tableView.separatorStyle = .None
        
        // headerView
        tableView.tableHeaderView = noLoginHeaderView
    }
    
    /// 懒加载，创建 未登录 headerView
    private lazy var noLoginHeaderView: HQMineNoLoginHeaderView = {
        let noLoginHeaderView = HQMineNoLoginHeaderView.noLoginHeaderView()
        noLoginHeaderView.delegate = self
        return noLoginHeaderView
    }()
    
    // MARK: - 设置状态栏的颜色为白色
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .LightContent
    }

}

// MARK: - UITableViewDataSource,UIScrollViewDelagate
extension HQMineViewController {
    
    // UITableViewDataSource
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return kMargin
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return cells.count ?? 0
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let cellArray = cells[section] as! [HQMineCellModel]
        return cellArray.count ?? 0
        
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(mineCellID) as! HQMineCell
        
        let cellArray = cells[indexPath.section] as! [HQMineCellModel]
        
        cell.cellModel = cellArray[indexPath.row]
        return cell
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        if indexPath.section == 0 {
            // 登录
            let loginVc = HQLoginViewController()
            navigationController?.pushViewController(loginVc, animated: true)
            
        } else if indexPath.section == 1 {
            if  indexPath.row == 1 {
                // 离线下载
                let offlineVC = HQOfflineTableViewController()
                navigationController?.pushViewController(offlineVC, animated: true)
                
            } else if indexPath.row == 2 {
                // 活动
                let activityVC = HQActivityController()
                navigationController?.pushViewController(activityVC, animated: true)
                
            } else if indexPath.row == 4 {
                // 离线
                let loginVc = HQLoginViewController()
                navigationController?.pushViewController(loginVc, animated: true)
            }
        }
    }
    
    // UIScrollViewDelagate
    
//    override func scrollViewDidScroll(scrollView: UIScrollView) {
//        let offsetY = scrollView.contentOffset.y
//        
//        if offsetY >= 0 {
//            
//            var tempFrame = noLoginHeaderView.bgImageView.frame
//            tempFrame.origin.y = offsetY
//            tempFrame.size.height = kHQMineHeaderImageHeight - offsetY
//            noLoginHeaderView.bgImageView.frame = tempFrame
//        }
//    }
    
}

// MARK: - HQMineNoLoginHeaderViewDelegate

extension HQMineViewController: HQMineNoLoginHeaderViewDelegate {
    // 手机登录按钮
    func noLoginHeaderView(headerView: HQMineNoLoginHeaderView, mobileLoginButtonClick: UIButton) {
        print(#function)
    }
    // 微信登录按钮
    func noLoginHeaderView(headerView: HQMineNoLoginHeaderView, wechatLoginButtonClick: UIButton) {
        print(#function)
    }
    // QQ登录按钮
    func noLoginHeaderView(headerView: HQMineNoLoginHeaderView, qqLoginButtonClick: UIButton) {
        print(#function)
    }
    // 微博登录按钮
    func noLoginHeaderView(headerView: HQMineNoLoginHeaderView, weiboLoginButtonClick: UIButton) {
        print(#function)
    }
    // 更多登录按钮
    func noLoginHeaderView(headerView: HQMineNoLoginHeaderView, moreLoginButtonClick: UIButton) {
        
        let loginVC = HQLoginViewController()
        navigationController?.pushViewController(loginVC, animated: true)
    }
    
    // 收藏按钮
    func noLoginHeaderView(headerView: HQMineNoLoginHeaderView, collectionBtnClick: HQVerticalButton) {
        let loginVC = HQLoginViewController()
        navigationController?.pushViewController(loginVC, animated: true)
    }
    
    // 夜间按钮
    func noLoginHeaderView(headerView: HQMineNoLoginHeaderView, nightBtnClick: HQVerticalButton) {
        print(#function)
    }
    
    // 设置按钮
    func noLoginHeaderView(headerView: HQMineNoLoginHeaderView, settingBtnClick: HQVerticalButton) {
        let loginVC = HQSettingViewController()
        navigationController?.pushViewController(loginVC, animated: true)
    }
    
}
